package com.dominator.serviceImpl;

import com.dominFramework.core.id.SystemIdUtils;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.mybatis.dao.*;
import com.dominator.repository.DaoGameGoods;
import com.dominator.service.ApiGameGoodsService;
import com.dominator.service.ApiOrderService;
import com.dominator.service.ApiUserService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;


@Slf4j
@Service("ApiGameGoodsService")
public class ApiGameGoodsServiceImpl implements ApiGameGoodsService {


    @Autowired
    DaoGameGoods gameGoodsDao;


    @Autowired
    T_user_game_goodsDao tUserGameGoodsDao;

    @Autowired
    T_game_goodsDao tGameGoodsDao;

    @Autowired
    ApiUserService apiUserService;

    @Autowired
    ApiOrderService apiOrderService;

    @Override
    public ApiMessage getMyGoodsList(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto, "userId,changedGroup");
        dto.setPageLimit(dto.getInteger("pageLimit"));
        dto.setPageStart(dto.getInteger("pageStart"));
        return ApiMessageUtil.success(gameGoodsDao.getMyGoodsListPage(dto));
    }

    @Override
    public ApiMessage sendUserGoods(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto, "userId,orderId");
        T_userPO tUserPO =apiUserService.getUserById(dto.getString("userId"));
        List<T_game_goodsPO> goodsList = gameGoodsDao.getGoodsList();
        if (SystemUtils.isNotEmpty(goodsList)){
            Random random =new Random();
             if (random.nextBoolean()){
                 //随机获取一个奖品
                 List<Dto>  resultGoodsList=null;
                 List<Integer> randomList = ApiUtils.getRandomQuestionNumList(1, goodsList.size());
                 Dto resDto=Dtos.newDto();
                 for (Integer integer:randomList){
                     resDto.clear();
                     resDto.put("goodId",goodsList.get(integer).getGame_id());
                     resDto.put("expDate",goodsList.get(integer).getExpiration_date());
                     resultGoodsList.add(resDto);
                 }
                 //添加
                 for (Dto pDto:resultGoodsList) {
                     T_user_game_goodsPO tUserGameGoodsPO = new T_user_game_goodsPO();
                     tUserGameGoodsPO.setId(SystemIdUtils.uuid());
                     tUserGameGoodsPO.setCreate_time(new Date());
                     tUserGameGoodsPO.setGame_id(dto.getString("gameId"));
                     tUserGameGoodsPO.setGoods_id(pDto.getString("goodId"));
                     tUserGameGoodsPO.setIs_changed("0");
                     tUserGameGoodsPO.setIs_valid("1");
                     tUserGameGoodsPO.setUser_id(tUserPO.getId());
                     tUserGameGoodsPO.setCodes(ApiUtils.CreateRandom(4));
                     tUserGameGoodsPO.setExpiration_date(pDto.getDate("expDate"));
                     tUserGameGoodsPO.setGame_order_id(dto.getString("orderId"));
                     tUserGameGoodsDao.insert(tUserGameGoodsPO);
                 }
             }
        }
        return ApiMessageUtil.success();
    }

    @Override
    public ApiMessage getUserGoodsList(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto, "userId,orderId");
        List<T_user_game_goodsPO> userGoodsList = gameGoodsDao.getUserGoodsList(dto);
        if (SystemUtils.isEmpty(userGoodsList)){
            sendUserGoods(dto);
        }
        userGoodsList = gameGoodsDao.getUserGoodsList(dto);
        Dto resDto =Dtos.newDto();
        resDto.put("prizeList",userGoodsList);
        return ApiMessageUtil.success(resDto);
    }
}
